<template>
  <div>
    <!-- 面包屑导航区开始 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>商品管理</el-breadcrumb-item>
      <el-breadcrumb-item>商品列表</el-breadcrumb-item>
    </el-breadcrumb>
    <!-- 面包屑导航区结束 -->

    <!-- 卡片视图区域开始 -->
    <el-card>
      <!-- 搜索与添加框 -->
      <el-row :gutter="20">
        <el-col :span="8">
            <!-- 输入文字搜索时要调用获取数据渲染的函数，清空搜索框内容时也要重新调取函数获得数据在重新渲染 -->
          <el-input placeholder="请输入内容" clearable v-model="queryInfo.query" @clear="getGoodsList()">
            <el-button slot="append" icon="el-icon-search" @click="getGoodsList()"></el-button>
          </el-input>
        </el-col>
        <el-col :span="4">
            <!-- 点击添加跳转到添加商品页面 -->
          <el-button type="primary" @click="goAddPage">添加商品</el-button>
        </el-col>
      </el-row>
      <!-- 搜索框结束 -->

      <!-- 渲染商品列表的table表格区域开始 -->
      <el-table :data="goodsList" border stripe>
        <el-table-column type="index"></el-table-column>
        <el-table-column label="商品名称" prop="goods_name"></el-table-column>
        <el-table-column label="商品价格" prop="goods_price" width="90px"></el-table-column>
        <el-table-column label="商品重量" prop="goods_weight" width="90px"></el-table-column>
        <!-- 时间格式化模板 -->
        <el-table-column label="商品创建时间" prop="add_time" width="140px">
          <!-- dateFormat过滤器名称 -->
          <template slot-scope="scope">{{scope.row.add_time | dateFormat }}</template>
        </el-table-column>
        <!-- 操作行-->
        <el-table-column label="操作" width="140px">
          <template slot-scope="scope">
            <el-button type="primary" size="mini" icon="el-icon-edit"></el-button>
            <el-button type="danger" size="mini" icon="el-icon-delete" @click="removeById(scope.row.goods_id)"></el-button>
          </template>
        </el-table-column>
      </el-table>
      <!-- 渲染商品列表的table表格区域结束 -->


      <!-- 分页功能区开始 -->
      <el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange" :current-page="queryInfo.pagenum"
        :page-sizes="[5, 10, 15, 20]"
        :page-size="queryInfo.pagesize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total" background></el-pagination>
      <!-- 分页功能区结束 -->
    </el-card>
    <!-- 卡片视图区域结束 -->
  </div>
</template>


<script>
export default {
  data() {
    return {
      //绑定的商品列表数据集合
      goodsList: [],
      //查询参数集合
      queryInfo: {
          //查询条件
        query:'',
        pagenum: 1,
        pagesize: 10
      },
      //总数据条数
      total: 0
    }
  },
  created() {
    //获取数据渲染页面
    this.getGoodsList()
  },
  methods: {
    //获取最初是数据请求
    async getGoodsList() {
      const { data: res } = await this.$http.get('goods', {
        params: this.queryInfo
      })

      if (res.meta.status !== 200) return this.$message.error('数据获取失败')
      this.goodsList = res.data.goods
      this.total = res.data.total
      console.log(this.goodsList)
      this.$message.success('获取数据成功')
    },
     //页面数据条数发生变化时会获取最新的数据
    handleSizeChange(newSize){
       this.queryInfo.pagesize=newSize
        //获取数据渲染页面
          this.getGoodsList()
    },
    handleCurrentChange(newPage){
        this.queryInfo.pagenum=newPage
         //获取数据渲染页面
         this.getGoodsList()
    },
    //根据商品id删除商品
   async removeById(goods_id){

        //提醒用户删除
            const confirmRes = await this.$confirm(
        '此操作将永久删除该用户, 是否继续?',
        '提示',
        {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }
      ).catch(err => err)
      // console.log(confirmRes)点击确定返回的结果是confirm,点击取消 返回的结果是concel
      if (confirmRes !== 'confirm') {
        return this.$message.info('该操作已经取消')
       }
        const {data:res} = await this.$http.delete(`goods/${goods_id}`)
         if(res.meta.status!==200)  return this.$message.error('删除失败 ')

          //获取数据渲染页面
         this.getGoodsList()
         //提示删除成功
         this.$message.success('删除成功 ')
    },
    //点击添加商品按钮利用编程式导航跳转到添加商品的页面
    goAddPage(){
      this.$router.push('/goods/add')
    },
  }
}
</script>

<style lang="less" scoped>
</style>